<div class="layui-fluid">
    <div class="layui-card">
        <!-- 搜索表单区 -->
        <form class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="generator_model-search-form">
            <div class="layui-form-item">

                <!-- 默认表单 -->
                <div class="layui-inline">
                    <label class="layui-form-label">生成器名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="generator_name" placeholder="请输入生成器名称" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <div class="layui-inline">
                    <button class="layui-btn btn-generator_model-search" lay-submit lay-filter="btn-generator_model-form-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>

        <!-- 列表区 -->
        <div class="layui-card-body">

            <!-- 头部工具栏模板 -->
            <script type="text/html" id="generator_model_header_toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn" lay-event="add">添加</button>
                    <button class="layui-btn layui-btn-danger" lay-event="dels">删除</button>
                </div>
            </script>

            <!-- 列表表格 -->
            <table id="generator_model-table" lay-filter="generator_model-table" style="width: 100%"></table>


            <!-- 每行操作按钮模板 -->
            <script type="text/html" id="generator_model_row_toolbar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="code"><i class="layui-icon layui-icon-app"></i>代码生成</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<!-- JS脚本区 -->
<script>
    layui.use(['table', 'form', 'layer', 'laydate', 'jquery', 'admin', 'formSelects'], function () {
        var form = layui.form, layer = layui.layer, laydate = layui.laydate, table = layui.table, admin = layui.admin, formSelects = layui.formSelects;

        form.render(null, 'generator_model-search-form');


        //渲染创建时间日期时间选择器
        laydate.render({elem: '#create_datetime_search_datetime', type: 'datetime'});


        // 表单搜索监听
        form.on('submit(btn-generator_model-form-search)', function (data) {
            console.log('generator_model-search-form', data)
            var field = data.field;
            //执行重载
            table.reload('generator_model-table', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });

        // 渲染表格数据
        table.render({
            id: 'generator_model-table',
            elem: '#generator_model-table',
            toolbar: '#generator_model_header_toolbar',
            url: '/generator/code/list',
            loading: true,
            page: true, //开启分页
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                // {field: 'generator_id', title: '生成器编号'/*, event: 'generator_id', templet: "#generator_id_templet"*/},
                {field: 'generator_name', title: '生成器名称'/*, event: 'generator_name', templet: "#generator_name_templet"*/},
                {field: 'generator_config', title: '生成器配置'/*, event: 'generator_config', templet: "#generator_config_templet"*/},
                {field: 'create_datetime', title: '创建时间'/*, event: 'create_datetime', templet: "#create_datetime_templet"*/},
                {title: '操作', width: 400, fixed: 'right', templet: '#generator_model_row_toolbar'}
            ]],
            done: function () {
            }
        });

        // 监听表格工具栏事件
        table.on("toolbar(generator_model-table)", eventListener);

        // 监听表格行事件
        table.on("tool(generator_model-table)", eventListener);

        // 事件监听
        function eventListener(obj) {
            let event = obj.event;
            let data = obj.data;
            switch (event) {
                case 'add':
                    layui.$.post('/generator/code/editPage', function (str) {
                        layer.open({
                            type: 1,
                            title: '新建代码生成器',
                            maxmin: true,
                            area: ['100%', '100%'],
                            content: str,
                        })
                    });
                    break;
                case 'preview':
                    layui.$.post('/generator/code/preview', {generator_id: data.generator_id}, function (str) {
                        layer.open({
                            type: 1,
                            title: '代码生成预览',
                            maxmin: true,
                            area: ['100%', '100%'],
                            content: str,
                        })
                    });
                    break;
                case 'edit':
                    layui.$.post('/generator/code/editPage', {generator_id: data.generator_id}, function (str) {
                        layer.open({
                            type: 1,
                            title: '编辑代码生成器',
                            maxmin: true,
                            area: ['100%', '100%'],
                            content: str,
                        })
                    });
                    break;
                case 'code':
                    layui.$.post('/generator/code/codePage', {generator_id: data.generator_id, port: -1}, function (str) {
                        layer.open({
                            type: 1,
                            title: '代码生成',
                            maxmin: true,
                            area: ['100%', '100%'],
                            content: str,
                            cancel: function (index, layero) {
                                let logSocket = window.logSocket;
                                if (logSocket) {
                                    logSocket.close();
                                }
                                layer.close(index);
                            }
                        })
                    });
                    break;
                case 'download':
                    window.open('/generator/code/download?generator_id=' + data.generator_id);
                    break;
                case 'dels':
                    let checkStatus = table.checkStatus('generator_model-table');
                    let selectRow = checkStatus.data;
                    deleteRow(selectRow);
                    break;
                case 'del':
                    let row = obj.data;
                    deleteRow([row]);
                    break;
            }
            console.log('toolbar_event', obj)
        }

        // 删除行方法
        function deleteRow(selectRows) {
            if (selectRows.length <= 0) {
                layer.msg('请先钩选要删除的记录行！');
                return;
            }
            layer.confirm('确定要删除这' + selectRows.length + '条记录吗？',
                {icon: 3, title: '提示', btn: ['确认删除', '取消']},
                function (index) {
                    console.log(index);
                    layui.$.ajax({
                        url: '/generator/code/delete',
                        data: {
                            ids: selectRows.map((row) => row.generator_id)
                        },
                        traditional: true,
                        type: 'POST',
                        success: function (result) {
                            if (result.success) {
                                layer.msg('删除成功', {icon: 6});
                                table.reload('generator_model-table');
                            } else {
                                layer.msg('删除失败！' + result.message, {icon: 5});
                            }
                        }
                    });
                });

        }

    })
</script>